# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
  def breadcrumb_for_category(category)
    breadcrumb = "<a href='/'>首页</a>"
    if category
      brand = Brand.find(params[:brand_id]) if params[:brand_id]
      category.ancestors().reverse!.each do |c|
        if params[:brand_id]
          if c.parent
            breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{params[:brand_id]}'>#{c.name}</a>"
          else
            breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.name}</a>"
          end
        else
          breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.name}</a>"
        end
        
        if !c.parent_id && params[:brand_id]
          breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{params[:brand_id]}'>#{brand.name}</a>"
        end
      end

      if params[:brand_id] && !category.parent_id
        breadcrumb += "><a href='/categories/#{category.id.to_s}'>#{category.name}</a>>#{brand.name}"
      else
        breadcrumb += ">#{category.name}"
      end
    end

    return breadcrumb
  end

  def breadcrumb_for_product(product)
    breadcrumb = "<a href='/'>首页</a>"

    if product
      product.category.ancestors().reverse.each do |c|    
        if !c.parent_id && product.brand_id
          breadcrumb += "><a href='/categories/#{c.id.to_s}'>#{c.name}</a>"
          breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{product.brand_id}'>#{product.brand.name}</a>"
        else
          breadcrumb += "><a href='/categories/#{c.id.to_s}?brand_id=#{product.brand_id}'>#{c.name}</a>"
        end
      end
      breadcrumb += "><a href='/categories/#{product.category.id.to_s}?brand_id=#{product.brand_id}'>#{product.category.name}</a>>#{product.name}"
    end

    return breadcrumb
  end

  def is_admin(user_id)
    user = user_id &&  User.find(user_id)
    if user
      user.has_role?('administrator')
    else
      false
    end
  end

  def use_tinymce
    @content_for_tinymce = ""
    content_for :tinymce do
      javascript_include_tag "tiny_mce/tiny_mce"
    end
    @content_for_tinymce_init = ""
    content_for :tinymce_init do
      javascript_include_tag "mce_editor"
    end
  end

  def format_boolean(value)
    value ? "<img src='/images/tick.png' alt='true'></img>":"<img src='/images/cross.png' alt='false'></img>"
  end

  def format_unit(value)
    (Category::UNITS.find {|s|s[1] == value})[0] if value
  end

  def format_article_type(value)
    (Article::TYPE.find {|s|s[1] == value})[0] if value
  end


  def menu_html
    if  Rails.cache.exist?('menu_html')
      return_html = Rails.cache.read('menu_html',return_html)
    else
      return_html = "<ul id='tree_menu'>"
      categories = Category.parent_cagegories.map{|c|"<li><a href='/categories/#{c.id}'>#{c.name}</a></li>"}
      return_html += categories.join
#      return_html +="<li>#{Time.now.to_s}</li></ul>"
      Rails.cache.write('menu_html',return_html)
    end
    return return_html
  end

  #  def display_categories(categories = Category.find(:all,:conditions=>"parent_id is null"))
  #    if cache_exist?('cate_menu')
  #      return
  #    else
  #      ret = "<ul id='tree_menu'>"
  #      for category in categories
  #        ret << display_category(category)
  #      end
  #      ret << "<li>#{Time.now.to_s}</li></ul>"
  #    end
  #
  #  end
  #
  #  def display_category(category)
  #    ret = "<li>"
  #    category_name = category.name
  #    category_name += ">" if category.children.size > 0
  #    ret << "<a href='/categories/#{category.id}'>#{category_name}</a>"
  #    ret << display_categories(category.children) if category.children.size > 0
  #    ret << "</li>"
  #  end

  def categories_options(categories = Category.find(:all,:conditions =>['parent_id is null and show_in_nav = 1']))
    options =Array.new
    for category in categories
      create_category_options(category,options)
    end
    return options
  end

  def create_category_options(category,options)
    optioin_item =Array.new
    optioin_item[1]= category.id.to_s
    optioin_item[0]= category.name_for_select
    options << optioin_item
    if category.children.length > 0
      category.children.each do |c|
        create_category_options(c,options)
      end
    end
  end
end
